<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: cats.php 2009-06-24 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage backend
 * @license GNU/GPL
 * @filesource
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.controller' );
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'helper.php' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_joomgpstracks'.DS.'tables');
/**
 * Controller Class Categories
 */
class JoomgpstracksControllerCats extends JoomgpstracksController  {
    /**
     *
     */
    function display()
    {

	    parent::display();
    }


    /**
     *
     * @global object $mainframe
     * @uses JoomgpstracksModelCat::saveCat
     * @return redirect
     *
     */
    function savecat()  {

        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $model = $this->getModel('cat');
        $model->saveCat();

        // redirect to cats overview
        $link = JRoute::_( "index.php?option=com_joomgpstracks&task=cats&controllers=cats",false);
        $this->setRedirect($link, JText::_('GPS_CAT_SAVED'));

    }

    /**
     * @uses JoomgpstracksModelCat::move
     * @return redirect
     */
    function orderup()  {

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model = $this->getModel('cat');
		$model->move(-1);

        $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&task=cats&controllers=cats', false));
    }

    /**
     * @uses JoomgpstracksModelCat::move
     *@return redirect
     */
    function orderdown()  {

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model = $this->getModel('cat');
		$model->move(1);

        $this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&task=cats&controllers=cats', false));
    }

        /**
         * @uses JoomgpstracksModelCat::saveorder
         * @return redirect
         */
	function saveorder()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel('cat');
		$model->saveorder($cid, $order);

		$this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&task=cats&controllers=cats', false ));
	}


        /**
         * @uses JoomgpstracksModelCat::publish
         * @return redirect
         */
	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('cat');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&task=cats&controllers=cats', false ));
	}

        /**
         * @uses JoomgpstracksModelCat::publish
         * @return redirect
         */
	function unpublish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('cat');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&task=cats&controllers=cats', false ));
	}

        /**
         * @uses JoomgpstracksModelCat::delete
         * @return redirect
         */
	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('cat');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( JRoute::_('index.php?option=com_joomgpstracks&task=cats&controllers=cats', false ));
	}
    /**
     * @uses JoomgpstracksModelCat::updateCat
     * @return redirect
     */
    function updatecat()  {
        // check the token
        JRequest::checkToken() or die( 'Invalid Token' );

        $model = $this->getModel('cat');
        $model->updateCat();
        
        // redirect to cats overview
        $link = JRoute::_( "index.php?option=com_joomgpstracks&task=cats&controllers=cats",false);
        $this->setRedirect($link, JText::_('GPS_CAT_SAVED'));

    }


}

?>
